<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv=X-UA-Compatible content="IE=edge"/>
    <meta name=viewport content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>班级信息列表管理</title>
    <link rel="stylesheet" href="../css/element/index.css"/>
    <link rel="stylesheet" href="../css/main.css"/>
    <link rel="stylesheet" href="../css/header.css"/>
    <link rel="stylesheet" href="../css/sidebar.css"/>
    <link rel="stylesheet" href="../css/right.css"/>
</head>
<body>
<!--班级-->
<div id="app">
    <!-- 表格开始 -->
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-s-grid"></i> 班级信息表格</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <!-- 表主体开始 -->
        <div class="container">
            <!-- 搜索开始 -->
            <div class="handle-box">
                <el-form :inline="true">
                    <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAll"
                               size="small">批量删除
                    </el-button>
                    <el-input v-model="formMap.major" placeholder="专业" class="handle-input mr10"></el-input>
                    <el-input v-model="formMap.name" placeholder="班级名称" class="handle-input mr10"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="fetchData" size="small">搜索</el-button>
                    <el-button type="success" icon="el-icon-plus" size="small" @click="showEditWindow">添加班级</el-button>
                    </el-button>
                </el-form>
            </div>
            <!-- 搜索结束 -->

            <!-- 弹窗开始 -->
            <!--编辑课程信息 -->
            <el-dialog title="编辑班级信息" :visible.sync="dialogVisible">
                <el-form :model="aclass">
                    <el-form-item label="班级号" :label-width="formWidth">
                        <el-input v-model="aclass.clsNo" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="班级名" :label-width="formWidth">
                        <el-input v-model="aclass.clsName" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="专业" :label-width="formWidth">
                        <el-input v-model="aclass.major" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                    <el-button type="primary" @click="save" size="small">确 定</el-button>
                </div>
            </el-dialog>
            <!-- 弹窗结束 -->
            <!-- 表格数据开始 -->
            <el-table ref="multipleTable"
                      :data="tableData" class="table"
                      tooltip-effect="dark" style="width: 100%"
                      @selection-change="handleSelectionChange">
                <!-- 多选框所在的列 -->
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="clsNo"
                        label="班级号"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="clsName"
                        label="班级名"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="tname"
                        label="班主任"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="major"
                        label="专业"
                        width="180">
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="200">
                    <template slot-scope="scope">
                        <el-button size="mini" type="info" @click="edit(scope.row.id)">修改
                        </el-button>
                        <el-button size="mini" type="danger" @click="del(scope.row.clsNo)">删除</el-button>
                        <el-button type="text"  icon="el-icon-plus" @click="select(scope.row.clsNo)">指定班主任</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!--抽屉开始-->
            <el-drawer
                    title="未安排班级的教师信息列表"
                    :visible.sync="table"
                    direction="rtl"
                    size="50%">
                <el-table :data="gridData">
                    <el-table-column property="tno" label="教师号" width="150"></el-table-column>
                    <el-table-column property="tname" label="姓名" width="150"></el-table-column>
                    <el-table-column property="tsex" label="性别" width="150"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="sure(scope.row.id)">选定班主任
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-drawer>
            <!--抽屉结束-->
            <!-- 分页开始 -->
            <div class="pagination">
                <el-pagination
                        @size-change="fetchData"
                        @current-change="fetchData"
                        :current-page.sync="currentPage"
                        :page-sizes="pageSizes"
                        :page-size.sync="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
            <!-- 分页结束 -->
        </div>
        <!-- 表主体结束 -->
    </div>
    <!-- 表格结束 -->
</div>


<script src="../js/vue.min.js"></script>
<script src="../js/element.js"></script>
<script src="../js/axios.min.js"></script>
<script src="../js/basic.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            selectTeacher:{
                nowClsNo:'',//当前班级号
                selectTeacherId:''//指定的教师id
            },
            tableData: [],//课程表格数据
            table: false,
            gridData: [],//抽屉教师数据
            multipleSelection: [],//批量删除
            dialogVisible: false,//弹窗窗口
            aclass: {
                id:''
            },//编辑课程信息
            formWidth: '120px',
            currentPage: 1, // 当前页码
            pageSize: 5, // 每页显示记录数
            pageSizes: [2, 3, 5, 10, 15, 20], // 选择每页显示数
            total: 0, // 总记录数
            formMap: {
                major:"",
                name:""
            }, // 用于搜索的对象
        },
        created: function () {
            // 异步获取课程数据
            this.fetchData();
        },
        computed: {//计算
        },
        methods: {
            // 点击添加显示编辑窗口
            showEditWindow() {
                this.aclass = {};
                this.dialogVisible = true;
            },
            // 获取数据
            fetchData() {
                _this = this;
                axios.post(`${httpUrl}/aclass/list?page=${this.currentPage}&size=${this.pageSize}`, this.formMap)
                    .then(res => {
                        _this.tableData = res.data.row;
                        _this.total = res.data.total
                    }).catch(err => {
                    this.$message({
                        showClose: true,
                        message: err.message,
                        type: 'error'
                    });
                });
            },
            // 添加或者修改课程
            save() {
                _this = this;
                axios.post(`${httpUrl}/aclass/${this.aclass.id == null ? 'add' : 'update'}`, this.aclass)
                    .then(res => {
                        let r = res.data;
                        if (r.code !== 200) {
                            this.$message({
                                showClose: true,
                                message: r.msg,
                                type: 'error'
                            });
                        } else {
                            this.$message({
                                showClose: true,
                                message: r.msg,
                                type: 'success'
                            });
                            // 关闭弹窗
                            _this.dialogVisible = false;
                            // 重新获取一次数据
                            _this.fetchData();
                        }
                    });
            },
            // 根据id查询
            edit(id) {
                _this = this;
                axios.get(httpUrl + '/aclass/getById?id=' + id)
                    .then(res => {
                        // 显示编辑窗口
                        _this.dialogVisible = true
                        // 给编辑窗口设置值
                        _this.aclass = res.data.data
                        // 重新获取一次数据
                        _this.fetchData();
                    }).catch(er => {
                    this.$message({
                        showClose: true,
                        message: er.message,
                        type: 'error'
                    });
                })
            },
            //查询当前空闲(未安排班级的)教师
            select(clsNo) {
              this.selectTeacher.nowClsNo = clsNo;
              this.table = true;//开启表格
                _this = this;
                axios.post(`${httpUrl}/teacher/noClsNoList`)
                    .then(res => {
                        _this.gridData = res.data.data;
                    }).catch(err => {
                    this.$message({
                        showClose: true,
                        message: err.message,
                        type: 'error'
                    });
                });
            },
            //指定当前空闲教师为此班级的班主任
            sure(id){
                this.selectTeacher.id = id;
                _this = this;
                this.$confirm('你确定要指定当前教师为该班级的班主任吗？', '确认', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post(`${httpUrl}/aclass/selectTeacher?`, this.selectTeacher)
                        .then(res => {
                            if (res.data.code === 200){
                                this.$message({
                                    type: 'success',
                                    message: res.data.msg
                                });
                            }else {
                                this.$message({
                                    type: 'error',
                                    message: res.data.msg
                                });
                            }
                            _this.fetchData();
                        });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            },
            // 删除
            del(clsNo) {
                _this = this;
                this.$confirm('你确定要删除么？', '删除确认', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get(`${httpUrl}/aclass/delete?clsNo=` + clsNo)
                        .then(res => {
                            this.$message({
                                type: 'success',
                                message: res.data.msg
                            });
                            _this.fetchData();
                        });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 批量删除
            delAll() {
                const length = this.multipleSelection.length;
                if (length != 0) {
                    let str = '';
                    for (let i = 0; i < length; i++) {
                        if (i < length - 1) {
                            str += this.multipleSelection[i].id + ',';
                        } else {
                            str += this.multipleSelection[i].id;
                        }
                    }
                    axios.get(`${httpUrl}/aclass/batchDel?ids=${str}`)
                        .then(res => {
                            this.$message.success(res.data.msg);
                            this.multipleSelection = [];
                            // 重新获取一次数据
                            _this.fetchData();
                        })
                } else {
                    this.$message.error('请选择要删除的学生！');
                }
            },
            // 搜索:当分页改变时搜索
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    })
</script>
</body>
</html>